const mongoose = require('mongoose')

const menuSchema = new mongoose.Schema(
  {
    path: { type: String }, // 访问路径
    type: {
      // 类型
      type: String,
      required: true,
      enum: ['MENU', 'BUTTON'],
      default: 'MENU',
      uppercase: true,
      trim: true
    }, // 类型 3选1
    title: String, // menu 标题
    component: String, // menu, 页面文件所在项目的位置
    redirect: String, // 跳转路径(url)
    icon: String, // 图标
    isHidden: {
      type: Boolean,
      default: false
    },
    menuType: {
      type: String,
      default: 'group'
    },
    parent: {
      // 上级菜单
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Menu'
    }, // 自关联
    code: String // 权限标识字符串
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
  }
)

menuSchema.virtual('children', {
  localField: '_id',
  ref: 'menu',
  foreignField: 'parent',
  justOne: false
})

const menu = mongoose.model('menu', menuSchema, 'menus')

module.exports = menu
